import network from "../../utils/network";
import util from "../../utils/util";

const app = getApp()

Page({
    data: {
        active: '3',
        myData: {},
        listData: [],
        source_data: [],
        year: null,
        month: null,
        selectUserLoading: false,
        userList: [],
        currentDate: new Date().getTime(),
        maxDate: new Date().getTime(),
        popShow: false,
        activeNames: [],
        activeName: ['1'],
        valve: true,
        end: false,
        request: {
            page: 1,
            page_size: 10,
            name: '',
            type: '',
            user_name: '',
            user_id: '',
        },
        flag: false
    },
    onLoad: function (options) {
        const date = new Date()
        this.setData({
            year: date.getFullYear(),
            month: date.getMonth() + 1,
        })
        this.getData()
        this.initSaleUserArr()
    },
    getData(add = true) {
        const urlList = ['queryCompanyMonthByMonth', 'queryCompanyMonthPlanByMonth', 'companyAdvanceList']
        const index = parseInt(this.data.active) - 1
        util.showLoading('加载中')
        if (index === 2 && add) {
            this.setData({
                'request.page': this.data.request.page + 1,
                flag: true
            })
        }
        network.reqPromise('GET', {
            uri: app.api[urlList[index]],
            params: Object.assign({year: this.data.year, month: this.data.month}, this.data.request)
        }).then(res => {
            if (index !== 2) {
                this.setData({
                    myData: res.data
                })
            } else {
                res.data.records.forEach(item => {
                    item.activeNames = []
                    item.active = ['1']
                    item.activeIndex = 0
                })
                this.setData({
                    listData: this.data.listData.concat(res.data.records),
                    source_data: res.data
                })
                this.setData({
                    end: this.data.listData.length >= res.data.total,
                    flag: false
                })
            }
            util.hideLoading()
        }).catch(err => {
            if (index !== 2 && add) {
                this.setData({
                    'request.page': this.data.request.page - 1,
                    flag: false
                })
            }
            util.showToast(err.message ? err.message : '加载失败')
            util.hideLoading()
        })
    },
    onTabChange(e) {
        this.setData({
            active: e.detail.name
        })
        if (e.detail.name === '3') {
            this.setData({
                request: {
                    page: 1,
                    page_size: 10,
                    name: '',
                    type: '',
                    user_name: '',
                    user_id: '',
                },
                listData: []
            })
        }
        this.getData(false)
    },
    onChange(e) {
        console.log(e)
    },
    search() {
        this.setData({
            'request.page': 1,
            listData: []
        })
        this.getData(false)
    },
    reset() {
        this.setData({
            request: {
                page: 1,
                page_size: 10,
                name: '',
                type: '',
                user_name: '',
                user_id: '',
            },
            listData: []
        })
        this.search()
    },
    // 输入框输change
    onInputChange(e) {
        const key = e.target.dataset.key;
        this.setData({
            [key]: e.detail
        })
    },
    selectUserConfirm(e) {
        this.setData({
            'request.user_name': e.detail.value.name,
            'request.user_id': e.detail.value.id,
        })
    },
    initSaleUserArr() {
        network.reqPromise('GET', {
            uri: app.api.userListByCode,
            params: {code: 'Salesperson'}
        }).then(res => {
            this.setData({
                userList: res.data,
                selectUserLoading: false
            })
        }).catch(() => {
            util.showToast('初始化销售员数据失败')
        })
    },
    showPop() {
        this.setData({
            popShow: true
        })
    },
    onConfirm(e) {
        const date = new Date(e.detail)
        this.setData({
            year: date.getFullYear(),
            month: date.getMonth() + 1,
            popShow: false
        })
    },
    onCancel() {
        this.setData({
            popShow: false
        })
    },
    onSearch() {
        if (this.data.active === '3') {
            this.setData({
                'request.page': 1,
                listData: []
            })
        }
        this.getData(false)
    },
    collChange(e) {
        this.setData({
            activeNames: e.detail
        })
    },
    searchCollChange(e) {
        this.setData({
            activeName: e.detail
        })
    },
    loadNextPage() {
        // 没有全部加载结束
        if (!this.data.end && !this.data.flag) {
            this.getData()
        }
    },
});
